package cn.edu.stu.syllabus.repository.booklocate;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import cn.edu.stu.syllabus.repository.booklocate.local.BookLocateEntry;

/**
 * Created by daidaijie on 2017/3/28.
 */

public class BookLocateConverter {

    public static List<BookLocateEntry> convert(String html) {
        List<BookLocateEntry> bookLocateEntries = new ArrayList<>();


        Element body = Jsoup.parseBodyFragment(html).body();
        Element table = body.select("table.tb").first();
        Elements items = table.getElementsByTag("tr");
        items.remove(0);
        for (Element tr : items) {
            BookLocateEntry bookLocateEntry = new BookLocateEntry();

            Elements tds = tr.getElementsByTag("td");
            Element span1 = tds.get(0).getElementsByTag("span").first();
            Element span2 = tds.get(5).getElementsByTag("span").first();
            Element td1 = tds.get(6);

            bookLocateEntry.setBook(span1.text());
            bookLocateEntry.setState(span2.text());
            bookLocateEntry.setType(td1.text());

            bookLocateEntries.add(bookLocateEntry);
        }

        return bookLocateEntries;
    }
}
